const reqInterface = require('../../../../api/models')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefreshing: true, // 刷新状态
    list: [],
    searchParams: {
      page: 1,
      limit: 10,
      room_id: 0
    }
  },

  onShow() {
    let self = this;
    self.getFresh()
    self.getHouse()
  },

  getHouse() {
    let self = this;
    reqInterface.GetHouses({page: 1, limit: 100,name: '', id: 0}).then(res => {
      self.setData({
        houseList: res
      })
    })
  },

  houseIdValue(e) {
    let self = this;
    self.setData({
      list: [],
      searchParams: {
        id: 0,
        page: 1,
        limit: 10,
        status: self.data.searchParams.status,
        house_id: e.detail.val
      },
    })
    self.getList(self.data.searchParams)
  },

  getList(val) {
    let self = this;
    reqInterface.GetFees(val).then(res => {
      if (!res || res.length == 0) {
        wx.showToast({
          title: '没有更多数据',
          icon: 'none'
        })
      }
      self.setData({
        list: res ? self.data.list.concat(res) : self.data.list.concat([]),
        isRefreshing: false
      })
    })
  },
  onSearch(e) {
    let self = this;
    self.setData({
      'searchParams.name': e.detail.value
    })
    self.getList(self.data.searchParams)
  },
  // 加拉加载
  onLoadMore() {
    let self = this;
    let page = self.data.searchParams.page + 1; //获取当前页数并+1
    self.setData({
      'searchParams.page': page, //更新当前页数
    })
    self.getList(self.data.searchParams); //重新调用请求获取下一页数据
  },

  // 下拉刷新
  onRefresherPulling(e) {
    this.setData({
      isRefreshing: true
    });
  },

  onRefresherRefresh(e) {
    this.getFresh()
  },

  // 操作完毕后刷新
  getFresh() {
    let self = this;
    self.setData({
      list: [],
      searchParams: {
        page: 1,
        limit: 10,
        house_id: 0,
        status: 0
      },
    })
      self.getList(self.data.searchParams);
  },

  statusValue(e) {
    let self = this;
    self.setData({
      list: [],
      'searchParams.status': Number(e.detail.val),
      'searchParams.house_id': self.data.searchParams.house_id
    })
    self.getList(self.data.searchParams)
  },
  sendOrder(e) {
    let self = this;
    wx.showModal({
      title: '发送账单',
      confirmText: '发送',
      cancelText: '关闭',
      success(res) {
        if (res.confirm) {
          reqInterface.PostFee({id: e.currentTarget.dataset.id, status: 2}).then(res => {
            wx.showToast({
              title: '下发成功',
              icon: 'none'
            })
            self.getFresh()
          })
        }
        if (res.cancel) {
          
        }
      }
    })
  },
})